<?php

namespace App\Admin\Controllers;

use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Http\Controllers\AuthController as BaseAuthController;
use Dcat\Admin\Http\Repositories\Administrator;
use Weiaibaicai\OperationLog\Models\OperationLog;

class AuthController extends BaseAuthController
{
    protected $view = 'admin.login';

    public function switchOnline()
    {
        if ($this->guard()->check()) {
            $user = Admin::user()->switchOnlineStatus();

            OperationLog::makeLog([
                '切换后状态' => $user->online ? '在线' : '离线'
            ], '用户切换登录状态');

            return $this->response()->data([
                'online_status' => $user->online,
            ])->success('切换状态成功');
        }

        return $this->response()->error('用户Token 已过期.即将刷新页面!')->refresh();
    }

    public function getLastClueDate()
    {
        $user = Admin::user();
        $clue = $user->clues()
            ->select([
                'id', 'dispatch_date'
            ])
            ->whereNotNull('dispatch_date')
            ->orderBy('dispatch_date', 'desc')
            ->first();

        return $this->response()->data([
            'last-date' => $clue ? $clue->dispatch_date : null,
        ]);

    }

    public function checkUnDispatchCount()
    {
        if ($this->guard()->check()) {
            $user = Admin::user();

            return $this->response()->data([
                'online_status' => $user->online,
            ])->success('切换状态成功')->refresh();
        }

        return $this->response()->error('用户Token 已过期.即将刷新页面!')->refresh();
    }

    /**
     * Model-form for user setting.
     *
     * @return Form
     */
    protected function settingForm(): Form
    {
        return new Form(new Administrator(), function (Form $form) {
            $form->action(admin_url('auth/setting'));

            $form->disableCreatingCheck();
            $form->disableEditingCheck();
            $form->disableViewCheck();

            $form->tools(function (Form\Tools $tools) {
                $tools->disableView();
                $tools->disableDelete();
            });


            $form->display('username', trans('admin.username'));
            $form->display('name', trans('admin.name'))->required();
            $form->display('crm_username', 'crm登录名')->required();
            $form->image('avatar', trans('admin.avatar'))->autoUpload();

            $form->password('old_password', trans('admin.old_password'));

            $form->password('password', trans('admin.password'))
                ->minLength(5)
                ->maxLength(20)
                ->customFormat(function ($v) {
                    if ($v == $this->password) {
                        return;
                    }

                    return $v;
                });
            $form->password('password_confirmation', trans('admin.password_confirmation'))->same('password');

            $form->ignore(['password_confirmation', 'old_password']);

            $form->saving(function (Form $form) {
                if ($form->password && $form->model()->password != $form->password) {
                    $form->password = bcrypt($form->password);
                }

                if (!$form->password) {
                    $form->deleteInput('password');
                }
            });

            $form->saved(function (Form $form) {
                return $form
                    ->response()
                    ->success(trans('admin.update_succeeded'))
                    ->redirect('auth/setting');
            });
        });
    }
}
